package com.example.spring.boot.rabbitmq.listener;

import com.example.spring.boot.rabbitmq.constant.TopicExchangeConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 主题队列监听器
 * </p>
 *
 * @author weixin
 * @date 2023/3/14 15:16
 */
@Slf4j
@Component
public class TopicQueueListener {

    private static final String FORMATTER = "yyyy-MM-dd HH:mm:ss";

    /**
     * topic: 主题交换机
     * @param message {@link String}
     */
    @RabbitHandler
    @RabbitListener(queues = TopicExchangeConstant.TOPIC_QUEUE)
    public void process(String message) {
        log.info("TopicQueueListener.process, 接收到消息：{}, 接收消息时间：{}",
                message,
                LocalDateTime.now().format(DateTimeFormatter.ofPattern(FORMATTER))
        );
    }

    @RabbitHandler
    @RabbitListener(queues = TopicExchangeConstant.TOPIC_QUEUE2)
    public void process2(String message) {
        log.info("TopicQueueListener.process2, 接收到消息：{}, 接收消息时间：{}",
                message,
                LocalDateTime.now().format(DateTimeFormatter.ofPattern(FORMATTER))
        );
    }

    @RabbitHandler
    @RabbitListener(queues = TopicExchangeConstant.TOPIC_QUEUE3)
    public void process3(String message) {
        log.info("TopicQueueListener.process3, 接收到消息：{}, 接收消息时间：{}",
                message,
                LocalDateTime.now().format(DateTimeFormatter.ofPattern(FORMATTER))
        );
    }

    @RabbitHandler
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(TopicExchangeConstant.TOPIC_QUEUE),
            exchange = @Exchange(value = TopicExchangeConstant.TOPIC_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = "topic.1"
    ))
    public void process4(String message) {
        log.info("TopicQueueListener.process4, 接收到消息：{}, 接收消息时间：{}",
                message,
                LocalDateTime.now().format(DateTimeFormatter.ofPattern(FORMATTER))
        );
    }

}